package com.freedom.admin.infrastructure.persistence.repository;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.admin.domain.repository.PermissionRepository;
import com.freedom.admin.infrastructure.persistence.assembler.PermissionAssembler;
import com.freedom.admin.infrastructure.persistence.mapper.SysPermissionMapper;
import com.freedom.admin.infrastructure.persistence.pojo.bo.PermissionBO;
import com.freedom.admin.infrastructure.persistence.pojo.po.SysPermissionPO;
import com.freedom.admin.infrastructure.persistence.pojo.query.PermissionPageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
public class PermissionRepositoryImpl implements PermissionRepository {

    private final SysPermissionMapper sysPermissionMapper;

    private final PermissionAssembler permissionAssembler;

    @Override
    public List<String> findByRoleCodes(Set<String> roleCodeList) {
        return sysPermissionMapper.listBtnPermByRoles(roleCodeList);
    }

    @Override
    public Page<PermissionBO> queryPermissionPage(PermissionPageQuery queryParams) {
        Page<PermissionBO> page = new Page<>(queryParams.getCurrent(), queryParams.getPageSize());
        return sysPermissionMapper.selectPermissionPage(page, queryParams);
    }

    @Override
    public List<PermissionBO> queryPermissions(Integer menuId) {
        List<SysPermissionPO> permissionPOList = sysPermissionMapper.selectPermissions(menuId);
        return permissionAssembler.toBO(permissionPOList);
    }

    @Override
    public PermissionBO findPermissionById(Integer permissionId) {
        SysPermissionPO sysPermissionPO = sysPermissionMapper.selectById(permissionId);
        return permissionAssembler.toBO(sysPermissionPO);
    }

    @Override
    public Boolean modifyPermission(PermissionBO permissionBO) {
        SysPermissionPO sysPermissionPO = permissionAssembler.toPO(permissionBO);
        return sysPermissionMapper.updatePermission(sysPermissionPO);
    }

    @Override
    public Boolean addPermission(PermissionBO permissionBO) {
        SysPermissionPO sysPermissionPO = permissionAssembler.toPO(permissionBO);
        return sysPermissionMapper.insertPermission(sysPermissionPO);
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        sysPermissionMapper.deleteBatchIds(ids);
    }
}
